/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.img;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;

/**
 *
 * @author Lucas
 */
public class Mediana implements Filtro {

    @Override
    public String getName() {
        return "Mediana";
    }

    @Override
    public Map<String, List> filtrar(String image) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        Mat imgGray = Highgui.imread("./src/data/img/" + image, 0);
        int colunas = imgGray.cols();
        int linhas = imgGray.rows();
        double media = 0;
        
        /*
         * Mediana
         */
        List<Double> mediana;
        for (int l = 1; l < linhas - 1; l++) {
            for (int c = 0; c < colunas - 1; c++) {
                mediana = new ArrayList<>();
                mediana.add((imgGray.get(l, c)[0]));
                mediana.add((imgGray.get(l-1, c)[0]));
                mediana.add((imgGray.get(l-1, c-1)[0]));
                mediana.add((imgGray.get(l, c-1)[0]));
                mediana.add((imgGray.get(l+1, c-1)[0]));
                mediana.add((imgGray.get(l+1, c)[0]));
                mediana.add((imgGray.get(l+1, c+1)[0]));
                mediana.add((imgGray.get(l, c+1)[0]));
                mediana.add((imgGray.get(l-1, c+1)[0]));             
                Collections.sort(mediana);
                imgGray.put(l, c, mediana.get(4));
            }
        }
        
        
        
        //Variaveis auxiliares
        double[] valorRGB;
        int a = 0;
        int valorArray;
        
        /*
         * pega os pixeis e salva num array list...
         */
        List<Integer> histGray = new ArrayList<>();
        for (int i = 0; i < 255; i++) {
            histGray.add(i, 0);
        }
        for (int i = 0; i < linhas; i++) {
            for (int j = 0; j < colunas; j++) {
                valorRGB = imgGray.get(i, j);
                a = (int) valorRGB[0];
                valorArray = histGray.get(a);
                valorArray += 1;
                histGray.set(a, valorArray);
            }
        }
        
        Map<String, List> valores = new HashMap();
        valores.put("Mediana", histGray);
        return valores;
    }
}
